package cn.zh.stan.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cn.zh.stan.BaseUrl;
import cn.zh.stan.ITem.Order;
import cn.zh.stan.R;
import cn.zh.stan.activity.CarActivity;
import cn.zh.stan.activity.FilOrder;
import cn.zh.stan.activity.OrderDetail;
import cn.zh.stan.activity.ToPay;
import cn.zh.stan.util.MyListView;
import cn.zh.stan.util.ToastUtil;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by WYN on 2018/2/13 0013.
 */

public class OrderAdapter extends BaseAdapter {
    Context context;
    ArrayList<Order> arrayList;
    LayoutInflater inflater;
    Dialog dialog;
    String uid="";
public OrderAdapter(Context context,ArrayList<Order> arrayList,String uid){
    this.context=context;
    this.arrayList=arrayList;
    inflater=LayoutInflater.from(context);
    this.uid=uid;
    }


    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int i) {
        return arrayList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
       }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
       Holder holder=null;
       if (null==view){
           holder=new Holder();
           view=inflater.inflate(R.layout.order_layout,null,false);
            holder.orlist=view.findViewById(R.id.orlist);
            holder.totaltxt=view.findViewById(R.id.totaltxt);
            holder.moneytxt=view.findViewById(R.id.moneytxt);
            holder.onetxt=view.findViewById(R.id.onetxt);
            holder.matxt=view.findViewById(R.id.matxt);

            view.setTag(holder);
          }else {
           holder= (Holder) view.getTag();
         }
        GAdapter gAdapter=new GAdapter(context,arrayList.get(i).getArrayList());
        holder.orlist.setAdapter(gAdapter);
         switch (arrayList.get(i).getTradeStatus()){
             case "0":
                 //待付款
                 holder.matxt.setVisibility(View.VISIBLE);
                 holder.onetxt.setText("关闭订单");
                 holder.matxt.setText("支付");
                 break;
             case "1":
                 //待发货
                 holder.onetxt.setText("再次购买");
                 holder.matxt.setVisibility(View.GONE);
                 break;
             case "2":
                 //已发货
                 holder.matxt.setVisibility(View.VISIBLE);
                 holder.onetxt.setText("再次购买");
                 holder.matxt.setText("确认收货");
                 break;
             case "99":
                 //已完成
                 holder.onetxt.setText("再次购买");
                 holder.matxt.setVisibility(View.GONE);
                 break;
             }
          holder.onetxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            switch (arrayList.get(i).getTradeStatus()){
                case "0":
                    //待付款
                    getDialog(i);
                    break;
                case "1":
                    //待发货
                   Intent intent=new Intent(context, CarActivity.class);
                    context.startActivity(intent);
                    break;
                case "2":
                    //已发货
                    Intent intent1=new Intent(context, CarActivity.class);
                    context.startActivity(intent1);
                    break;
                case "99":
                    //已完成
                    Intent intent2=new Intent(context, CarActivity.class);
                    context.startActivity(intent2);
                    break;
                  }
               }
           });
          holder.matxt.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  switch (arrayList.get(i).getTradeStatus()){
                      case "0":
                          //待付款
                         Intent intent=new Intent(context, ToPay.class);
                         intent.putExtra("tradeNo",arrayList.get(i).getTradeNo());
                         intent.putExtra("tomon",arrayList.get(i).getGoodsTotal());
                         context.startActivity(intent);
                         break;
                      case "2":
                          //已发货,确认收货
                          getsure(i);

                          break;

                       }
                 }
          });
       final int j=i;
        holder.orlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent();
                intent.setClass(context, OrderDetail.class);
                intent.putExtra("cd",arrayList.get(j));
                context.startActivity(intent);
            }
        });
        return view;
        }

    class Holder{
     MyListView orlist;
     TextView totaltxt,moneytxt,onetxt,matxt;
     }

    public void getDialog(final int i){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.clodialog, null);// 得到加载view
        LinearLayout layou = (LinearLayout) v.findViewById(R.id.clo);// 加载布局
        TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("是否要关闭订单");
        dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layou,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               dialog.dismiss();
              }
           });

        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              //确定关闭订单
                closeorder(arrayList.get(i).getId(),i);
             }
          });
         }

    public void getsure(final int i){
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.clodialog, null);// 得到加载view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.clo);// 加载布局

        dialog = new Dialog(context, R.style.MyDialogStyle);
        dialog.show();
        dialog.setContentView(layout,new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
       TextView txt=v.findViewById(R.id.dgtxt);
        txt.setText("是否确认收货");
        v.findViewById(R.id.ocancle).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.dismiss();
            }
        });

        v.findViewById(R.id.osure).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //确定收货
                getReceive(i);
             }
           });
          }




    public  void closeorder(String id, final int i){
        dialog.dismiss();
        OkHttpUtils.post(BaseUrl.CloseOrder)
                .params("userId",uid)
                .params("id",id)
                .execute(new StringCallback() {
                    @Override
                    public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            boolean State=js.getBoolean("State");
                            if (State){
                                boolean Flag=js.getBoolean("Flag");
                                String Msg=js.getString("Msg");
                                if (Flag){
                                    arrayList.remove(i);
                                    notifyDataSetChanged();
                                }else {
                                    ToastUtil.showLong(context,Msg);
                                }
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                         }
                        }
                    @Override
                    public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                        super.onError(isFromCache, call, response, e);
                    }
                });
               }

        public void getReceive(final int i){
            dialog.dismiss();
         OkHttpUtils.post(BaseUrl.Receive)
                 .params("tradeNo",arrayList.get(i).getTradeNo())
                 .execute(new StringCallback() {
                     @Override
                     public void onResponse(boolean isFromCache, String s, Request request, @Nullable Response response) {
                         JSONObject js= null;
                         try {
                             js = new JSONObject(s);
                             boolean State=js.getBoolean("State");
                             if (State){
                                 boolean Flag=js.getBoolean("Flag");
                                 String Msg=js.getString("Msg");
                                 if (Flag){
                                    arrayList.remove(i);
                                    notifyDataSetChanged();
                                 }else {
                                     ToastUtil.showLong(context,Msg);
                                 }
                             }
                         } catch (JSONException e) {
                             e.printStackTrace();
                         }

                       }
                     @Override
                     public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
                         super.onError(isFromCache, call, response, e);
                     }
                 });
                 }


             }
